package com.technology.education.base

import android.content.Context
import com.google.gson.Gson
import com.technology.education.data.AppData
import com.technology.education.data.SpKeys
import com.technology.education.data.model.CallEventModel
import com.technology.education.data.model.VideoCallModel
import com.technology.education.ext.postEventBus
import com.technology.education.ext.showLog
import com.technology.education.ext.toast
import com.tencent.android.tpush.*
import com.tencent.liteav.trtccalling.model.TRTCCalling
import com.tencent.liteav.trtccalling.model.impl.base.CallModel
import com.tencent.liteav.trtccalling.model.impl.base.SignallingData
import me.hgj.jetpackmvvm.base.appContext

/**
 *@author:create by BZY
 *Date: 2022/4/24 17
 */
class MyPushReceiver : XGPushBaseReceiver() {
    override fun onRegisterResult(p0: Context?, p1: Int, p2: XGPushRegisterResult?) {
        showLog("onRegisterResult--$p1--${p2?.toJson()}")
    }

    override fun onUnregisterResult(p0: Context?, p1: Int) {

    }

    override fun onSetTagResult(p0: Context?, p1: Int, p2: String?) {

    }

    override fun onDeleteTagResult(p0: Context?, p1: Int, p2: String?) {

    }

    override fun onSetAccountResult(p0: Context?, p1: Int, p2: String?) {

    }

    override fun onDeleteAccountResult(p0: Context?, p1: Int, p2: String?) {

    }

    override fun onSetAttributeResult(p0: Context?, p1: Int, p2: String?) {

    }

    override fun onQueryTagsResult(p0: Context?, p1: Int, p2: String?, p3: String?) {

    }

    override fun onDeleteAttributeResult(p0: Context?, p1: Int, p2: String?) {

    }

    override fun onTextMessage(p0: Context?, p1: XGPushTextMessage?) {
        showLog("onRegisterResult----${p1?.toString()}")
        p1?.let {
            when (it.title) {
                SpKeys.REFRESH_ORDER_DETAIL -> postEventBus(SpKeys.REFRESH_ORDER_DETAIL, it.content)
                SpKeys.CHANGE_ORDER_STATUS_WAIT_EVALUATE -> postEventBus(
                    SpKeys.CHANGE_ORDER_STATUS_WAIT_EVALUATE,
                    it.content
                )
                SpKeys.VIDEO_CALL -> {
                    val model = Gson().fromJson(it.content, VideoCallModel::class.java)
                    AppData.CURRENT_INVITER = model.inviter
                    AppData.CURRENT_INVITE_ID=model.videoModel.callid
                    TRTCCalling.sharedInstance(appContext).handleRecvCallModel(
                        model.videoModel.callid, model.inviter, "",
                        listOf(model.invitee), Gson().toJson(model.videoModel)
                    )
                }
                SpKeys.SHOW_LOCAL -> {
                    ///因为信息都是推给学生,这里停止屏幕
                    TRTCCalling.sharedInstance(appContext).stopScreenCapture()
                }

                SpKeys.SHOW_REMOTE -> {
                    ///因为信息都是推给学生,这里开启屏幕共享
                    TRTCCalling.sharedInstance(appContext).startScreenCapture()
                }
                SpKeys.VIDEO_HANG_UP -> {
                    val data = Gson().fromJson(it.content, CallEventModel::class.java)
                    when (data.type) {
                        CallModel.VIDEO_CALL_ACTION_ACCEPT -> TRTCCalling.sharedInstance(appContext)
                            .onInviteeAccepted(data.inviteId, data.invitee, data.data)
                        CallModel.VIDEO_CALL_ACTION_SPONSOR_CANCEL -> {
                            toast("对方提取消视频会话")
                            TRTCCalling.sharedInstance(
                                appContext
                            ).onInvitationCancelled(data.inviteId, data.invitee, data.data)
                        }
                        CallModel.VIDEO_CALL_ACTION_REJECT -> {
                            toast("对方已拒绝视频会话")
                            TRTCCalling.sharedInstance(appContext)
                                .onInviteeRejected(data.inviteId, data.invitee, data.data)
                        }
                        CallModel.VIDEO_CALL_ACTION_LINE_BUSY -> {
                            toast("对方忙线中")
                            TRTCCalling.sharedInstance(appContext)
                                .onInviteeRejected(data.inviteId, data.invitee, data.data)
                        }
                        CallModel.VIDEO_CALL_ACTION_SPONSOR_TIMEOUT -> {
                            toast("对方忙线中")
                            TRTCCalling.sharedInstance(appContext)
                                .onInvitationTimeout(data.inviteId, listOf(data.invitee))
                        }
                        CallModel.VIDEO_CALL_ACTION_HANGUP -> {
                            toast("对方已挂断")
                            TRTCCalling.sharedInstance(appContext)
                                .onInvitationCancelled(data.inviteId, data.invitee, data.data)
                            postEventBus(SpKeys.REFRESH_ORDER_DETAIL, it.content)
                        }
                        else -> {
                            postEventBus(SpKeys.REFRESH_ORDER_DETAIL, it.content)
                        }
                    }
                }
            }
        }
    }

    override fun onNotificationClickedResult(p0: Context?, p1: XGPushClickedResult?) {

    }

    override fun onNotificationShowedResult(p0: Context?, p1: XGPushShowedResult?) {

    }
}